package com.example.fragmentdemo;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;

public class MainActivity extends Activity implements OnClickListener {
	private int currentTab;

	private ChaXunFragment chaxunFragment;

	private LuRuFragment luruFragment;

	private MyselfFragment myselfFragment;

	private View luRuLayout;

	private View chaXunLayout;

	private View myselfLayout;

	private ImageView luRuImage;

	private ImageView chaXunImage;

	private ImageView myselfImage;

	private TextView luRuText;

	private TextView chaXunText;

	private TextView myselfText;

	private FragmentManager fragmentManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		initViews();
		fragmentManager = getFragmentManager();

		setTabSelection(0);
	}

	private void initViews() {
		luRuLayout = findViewById(R.id.luru_layout);
		chaXunLayout = findViewById(R.id.chaxun_layout);
		myselfLayout = findViewById(R.id.myself_layout);
		luRuImage = (ImageView) findViewById(R.id.luru_image);
		chaXunImage = (ImageView) findViewById(R.id.chaxun_image);
		myselfImage = (ImageView) findViewById(R.id.myself_image);
		luRuText = (TextView) findViewById(R.id.luru_text);
		chaXunText = (TextView) findViewById(R.id.chaxun_text);
		myselfText = (TextView) findViewById(R.id.myself_text);
		luRuLayout.setOnClickListener(this);
		luRuLayout.setOnClickListener(this);
		chaXunLayout.setOnClickListener(this);
		myselfLayout.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.luru_layout:
			setTabSelection(0);
			break;
		case R.id.chaxun_layout:
			setTabSelection(1);
			break;
		case R.id.myself_layout:
			setTabSelection(2);
			break;

		default:
			break;
		}
	}

	private void setTabSelection(int index) {

		clearSelection();

		FragmentTransaction transaction = fragmentManager.beginTransaction();

		hideFragments(transaction);
		switch (index) {
		case 0:
			// obtainFragmentTransaction(0);
			luRuImage.setImageResource(R.drawable.luru_xuanzhong);
			luRuText.setTextColor(Color.WHITE);
			if (luruFragment == null) {

				luruFragment = new LuRuFragment();
				transaction.add(R.id.content, luruFragment);
			} else {

				transaction.show(luruFragment);
			}
			break;
		case 1:
			// obtainFragmentTransaction(1);
			chaXunImage.setImageResource(R.drawable.chaxun_xuanzhong);
			chaXunText.setTextColor(Color.WHITE);
			if (chaxunFragment == null) {

				chaxunFragment = new ChaXunFragment();
				transaction.add(R.id.content, chaxunFragment);
			} else {

				transaction.show(chaxunFragment);
			}
			break;
		case 2:
			// obtainFragmentTransaction(2);
			myselfImage.setImageResource(R.drawable.wo_xuanzhong);
			myselfText.setTextColor(Color.WHITE);
			if (myselfFragment == null) {

				myselfFragment = new MyselfFragment();
				transaction.add(R.id.content, myselfFragment);
			} else {

				transaction.show(myselfFragment);
			}
			break;

		default:
		}
		transaction.commit();
	}

	private void clearSelection() {
		luRuImage.setImageResource(R.drawable.ic_menu_edit);
		luRuText.setTextColor(Color.parseColor("#82858b"));
		chaXunImage.setImageResource(R.drawable.ic_menu_search);
		chaXunText.setTextColor(Color.parseColor("#82858b"));
		myselfImage.setImageResource(R.drawable.contacts_unselected);
		myselfText.setTextColor(Color.parseColor("#82858b"));
	}

	private void hideFragments(FragmentTransaction transaction) {
		if (luruFragment != null) {
			transaction.hide(luruFragment);
		}
		if (chaxunFragment != null) {
			transaction.hide(chaxunFragment);
		}
		if (myselfFragment != null) {
			transaction.hide(myselfFragment);
		}
	}
	// private FragmentTransaction obtainFragmentTransaction(int index){
	// FragmentTransaction ft = getFragmentManager().beginTransaction();
	// // 设置切换动画
	// if(index > currentTab){
	// ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
	// }else{
	// ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out);
	// }
	// return ft;
	// }
}
